<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>品牌信息</title>
    <script src="js/axios.min.js"></script>

    <style>


        body {

            margin: 0 auto;
            font-size: 16px;
        }

        .formDiv {

            margin: auto;

            width: 500px;
            height: 300px;
        }

        .formDiv li {

            list-style: none;
            line-height: 30px;
        }


    </style>
</head>

<script>

    //判断操作是新增还修改
    window.onload = function () {
        const queryParams = new URLSearchParams(window.location.search);
        const id = queryParams.get('id');



        if (id != null || id != 'undefined') //是修改操作
        {
            //发送查询
            axios({url: '/brand-case/BrandGetServlet?id='+id, method: 'GET'}).then((resp) => {

                var brandFormData = resp.data
                //表单回显
                document.getElementById('brandName').value = brandFormData.brandName;
                document.getElementById('companyName').value=brandFormData.companyName;
                document.getElementById('ordered').value=brandFormData.ordered;
                document.getElementById('description').value=brandFormData.description;
                document.getElementById('brandId').value=brandFormData.id;
                let statuss = document.getElementsByName("status");

                for (let i = 0; i < statuss.length; i++) {
                    if (statuss[i].value==brandFormData.status) {
                        statuss[i].checked = true
                    }
                }
            })


        }
    }

    //提交时
    function beforSub() {

        //JSON字符串 : 前段的品牌信息封装JavaScript对象
        var brandData = {
            id:'',
            brandName: '',
            companyName: '',
            ordered: '',
            description: '',
            status: ''
        }


        //2.获取表单 属性值
        brandData.brandName = document.getElementById('brandName').value
        brandData.id = document.getElementById('brandId').value
        brandData.companyName = document.getElementById('companyName').value
        brandData.ordered = document.getElementById('ordered').value
        brandData.description = document.getElementById('description').value
        let statuss = document.getElementsByName("status");

        for (let i = 0; i < statuss.length; i++) {
            if (statuss[i].checked) {
                brandData.status = statuss[i].value;
            }
        }


        //3.发送请求

        //判断新增还是修改

        axios({url: '/brand-case/BrandAddOrUpdateServlet', method: 'POST', data: brandData}).then(
            (resp)=>{


                if(resp.data == "OK")

                {



                  window.location.href='brand.html'
                }else{


                    alert("操作失败!!!")
                }
            }
        )

    }


</script>

<body>

<div class="formDiv">

    <h2>品牌信息</h2>


        <input id="brandId" type="hidden">
        <li><span>品牌名称</span> <input type="text" id="brandName" name="brandName"></li>
        <li><span>企业名称</span> <input type="text" id="companyName" name="companyName"></li>
        <li><span>排&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;序</span>
            <input type="text" name="ordered" id="ordered"></li>
        <li><span>品牌介绍</span><br>
            <textarea rows="10" cols="50" id="description" name="description"></textarea></li>

        <li><span>状态</span>
            <input type="radio" name="status" value="0">禁用
            <input type="radio" name="status" value="1" checked>启用
        </li>


        <li>
            <input type="submit" onclick="beforSub()" value="提交">
            <input type="reset" value="重置">
        </li>


</div>
</body>
</html>